package com.dianrui.agent.activity;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.dianrui.agent.R;
import com.dianrui.agent.okhttp.OkHttpRequest;
import com.dianrui.agent.util.SharedUtil;
import com.dianrui.agent.util.StringUtils;
import com.dianrui.agent.util.ToastUtil;
import com.dianrui.agent.util.Url;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

public class ForgetLoginPwd extends BaseActivity {
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.act_edit_login_pwd_et_tel)
    EditText etTel;
    @BindView(R.id.act_edit_login_pwd_et_psw_old)
    EditText etOldOne;
    @BindView(R.id.act_edit_login_pwd_et_psw_one)
    EditText etPswOne;
    @BindView(R.id.act_edit_login_pwd_et_psw_two)
    EditText etPswTwo;


    @Override
    public int getContentViewId() {
        return R.layout.activity_forget_pwd;
    }

    @Override
    public void initView() {
        title.setText("修改账户密码");
        if (!StringUtils.isEmpty(spUtils.getString("mobile"))) {
            etTel.setText(spUtils.getString("mobile"));
        }
    }


    @OnClick({R.id.back, R.id.update_pwd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.update_pwd:
                String mobile = etTel.getText().toString().trim();//手机
                String oldpwd = etOldOne.getText().toString();//旧密码
                String psw1 = etPswOne.getText().toString().trim();//新密码
                String psw2 = etPswTwo.getText().toString().trim();//确认新密码

                if (StringUtils.isEmpty(mobile)) {
                    ToastUtil.showToast("请输入手机号");
                    return;
                }
                if (StringUtils.isEmpty(oldpwd)) {
                    ToastUtil.showToast("请输入旧密码");
                    return;
                }
                if (StringUtils.isEmpty(psw1)) {
                    ToastUtil.showToast("请输入新密码");
                    return;
                }
                if (StringUtils.isEmpty(psw2)) {
                    ToastUtil.showToast("请输入确认密码");
                    return;
                }

                if (!psw1.equals(psw2)) {
                    ToastUtil.showToast("两次密码不一致,请重新确认");
                    return;
                }
                modifyLoginPwd();
                break;

            default:
                break;
        }
    }

    public void modifyLoginPwd() {
        JsonObject forgetpwd = new JsonObject();
        forgetpwd.addProperty("old_password", etOldOne.getText().toString());
        forgetpwd.addProperty("new_password", etPswOne.getText().toString());
        OkHttpRequest.getInstance().postTokenRequests(SharedUtil.getUrl() + Url.FORGETPWD, forgetpwd.toString(), new OkHttpRequest.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if ("1000".equals(jsonObject.optString("status"))) {
                            spUtils.clear();
                            ToastUtil.showToast(jsonObject.optString("message"));
                            JumpActivitys(LoginActivity.class);
                            finish();
                        } else if ("1001".equals(jsonObject.optString("status"))) {
                            ToastUtil.showToast(jsonObject.optString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void FailedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }
}
